package com.haina.vote.controller;
import com.github.pagehelper.PageInfo;
import com.haina.vote.entity.IpInfo;
import com.haina.vote.entity.Vote;
import com.haina.vote.entity.VoteItem;
import com.haina.vote.service.IVoteService;
import com.haina.vote.utils.IPAddressUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Controller
public class VoteController {
    @Autowired
    private IVoteService voteService;
    //查询所有投票
    @RequestMapping("/findAllVote.do")
    public ModelAndView findAllVote(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,@RequestParam(name = "pageSize",required = true,defaultValue = "10")Integer pageSize){
        List<Vote>votes=voteService.findAllVote(page,pageSize);
        PageInfo pageInfo=new PageInfo(votes);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("vote-list");
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }
    //新建投票
    @RequestMapping("/addVote.do")
    public String addVote(Vote vote){
        vote.setCreate_time(new Date());
        voteService.addVote(vote);
        return "redirect:findAllVote.do";
    }
    //渲染投票的修改页面
    @RequestMapping("/updateVoteUI.do")
    public ModelAndView updateVoteUI(@RequestParam("id")Integer id){
       Vote vote = voteService.findVoteById(id);
       ModelAndView mv = new ModelAndView();
       mv.setViewName("vote-edit");
       mv.addObject("vote",vote);
       return mv;
    }
    //修改投票
    @RequestMapping("/updateVote.do")
    public String updateVote(Vote vote){
        voteService.updateVote(vote);
        return "redirect:findAllVote.do";
    }
    //删除整个投票 先删除投票下的每个选项
    @RequestMapping("/deleteVote.do")
    public String deleteVote(@RequestParam("id")Integer id){
        voteService.deleteVote(id);
        return "redirect:findAllVote.do";
    }
    //查询所有
    @RequestMapping("/findAllItem.do")
    public ModelAndView findAllItem(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,@RequestParam(name = "pageSize",required = true,defaultValue = "10")Integer pageSize){
        List<VoteItem> voteItems=voteService.findAllItem(page,pageSize);
        PageInfo pageInfo=new PageInfo(voteItems);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("voteItem-list");
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }

    //查询投票信息并生成对应类型的页面
    @RequestMapping("/voteInfo.do")
    public ModelAndView findVoteInfo(Integer id,Integer type){
      //查询所有选项
      List<VoteItem> voteItems = voteService.findVoteInfo(id);
      //查询投票信息
      Vote vote = voteService.findVoteById(id);
      ModelAndView mv = new ModelAndView();
      mv.setViewName("vote-show");
      mv.addObject("voteItems",voteItems);
      mv.addObject("type",type);
      mv.addObject("vote",vote);
      return mv;
    }

    //跳转到选项添加页面
    @RequestMapping("/itemSaveUI")
    public ModelAndView itemSaveUI(){
       List<Vote> voteItems = voteService.findvote();
       ModelAndView mv = new ModelAndView();
       mv.setViewName("voteItem-add");
       mv.addObject("voteItems",voteItems);
       return mv;
    }

    //创建选项
    @RequestMapping("/addItem.do")
    public String saveVoteItem(HttpServletRequest request,MultipartFile upload,VoteItem voteItem) throws Exception {
        //使用fileupload组件完成文件上传
        //上传位置
        String path =request.getSession().getServletContext().getRealPath("/uploads/");
        String filename=null;
        //判断路径是否存在
        File file=new File(path);
        if (!file.exists()){
            file.mkdirs();
        }
        filename = upload.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-","");
        filename=uuid+"_"+filename;
        //完成文件上传
        upload.transferTo(new File(path,filename));
        String fileurl="/uploads/"+filename;
        voteItem.setFile_url(fileurl);
        System.out.println(voteItem);
        voteService.addItem(voteItem);
        return "redirect:findAllItem.do";
    }
    //修改投票选项1、先通过id查询该选项的信息并渲染的修改页面3、修改
    //查询需要修改的选项
    @RequestMapping("/updateItemUI.do")
    public ModelAndView findUpdateItemById(@RequestParam("item_id")Integer item_id){
        VoteItem voteItem=voteService.findItemById(item_id);
        List<Vote> voteInfos = voteService.findvote();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("voteItem-edit");
        mv.addObject("voteItem",voteItem);
        mv.addObject("voteInfos",voteInfos);
        return mv;
    }
    @RequestMapping("/updateItem.do")
    public String updateVoteItem(VoteItem voteItem) throws Exception {
        voteService.updateItem(voteItem);
        return "redirect:findAllItem.do";
    }

    //点击按钮进行投票
    @RequestMapping("/updateTimes.do")
    public String updateTimes(HttpServletRequest request,@RequestParam(name = "item_id") Integer item_id,@RequestParam(name = "vote_id")Integer vote_id){
        String ipAddress = request.getHeader("x-forwarded-for");
        //调用ip处理的工具类
        String ip = IPAddressUtils.GetIP(request,ipAddress);
        //首先根据ip和问卷id进行查询
        IpInfo ipInfo = voteService.findIpInfo(ip,vote_id);
        if (ipInfo==null){
            //创建记录并提示投票成功
            IpInfo ipInfo1=new IpInfo();
            ipInfo1.setIpaddress(ip);
            ipInfo1.setNumber(1);
            ipInfo1.setVote_id(vote_id);
            ipInfo1.setRequest_time(new Date());
            voteService.addIPInfo(ipInfo1);
        }else{
            //获取ip请求过的次数
            Integer req_num = ipInfo.getNumber();
            Integer ip_id=ipInfo.getId();
            if (req_num!=null){
                if (req_num>=10){
                    return "error01";
                }else{
                    req_num++;
                    voteService.updateIpInfo(req_num,ip_id);
                    //查询选项当前的票数
                    Integer times = voteService.findItemTimes(item_id);
                    times++;
                    voteService.updateVoteItemTimes(item_id,times);
                    return "success01";
                }
            }
        }
        return "redirect:voteInfo.do";
    }
    //删除投票选项
    @RequestMapping("/deleteItem.do")
    public String deleteItem(@RequestParam("item_id")Integer item_id){
        voteService.deleteItem(item_id);
        return "redirect:findAllItem.do";
    }
    //查询投票选项的详情
    @RequestMapping("/findItemById.do")
    public ModelAndView findItemById(@RequestParam("item_id")Integer item_id){
        VoteItem voteItem=voteService.findItemById(item_id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("voteItem-show");
        mv.addObject("voteItem",voteItem);
        return mv;
    }


}
